package org.willthink.common;

import org.apache.log4j.Logger;

public class PageUtil {
private Logger logger = Logger.getLogger(getClass());
	
	public String NavigationUtil (int totCnt , int cPage, int pageSize , int blockSize){
		logger.debug("PageUtil  "+" / totCnt : "+ totCnt+" / cPage : "+cPage+" / pageSize : "+pageSize+" / blockSize" );
		StringBuffer navigation = new StringBuffer();
		
		int totalPage = (int) Math.ceil(totCnt/pageSize);
		int totalBlock = (int)Math.ceil(totalPage/blockSize);
		int currentBlock = (int)Math.ceil((double)cPage/(double)blockSize);
		int firstPage = (currentBlock-1)*blockSize+1;
		int lastPage = firstPage*blockSize;;
		logger.debug("Page Info   totalPage"+totalPage +"/ totalBlock" +totalBlock+ " / currentBlock "+currentBlock + " / firstPage"+ firstPage+" / lastPage"+lastPage);
		if(totalBlock <= currentBlock)	lastPage	= totalPage;
		//첫페이지로
		if(currentBlock > 1){
			navigation.append("<a href=\"javascript:free_GoPage('" + 1 + "');\" ><img src=\"" +  "/img/mms/btn_page_first.gif\" alt=\"처음페이지\" width=\"14\" height=\"12\" align=\"absmiddle\" /></a>");
		}
		//이전 블럭 마지막 페이지로
		if(currentBlock > 1){
			navigation.append("<a href=\"javascript:free_GoPage('" + (currentBlock-1)*blockSize + "');\" ><img src=\"" +  "/img/mms/btn_page_first.gif\" alt=\"처음페이지\" width=\"14\" height=\"12\" align=\"absmiddle\" /></a>");
		}
		
		//페이지 블럭
		for(int i=firstPage; i<lastPage;i++){
			if(i>(firstPage+1))
				navigation.append(" ");
			if(i==cPage){
				navigation.append( "<strong style=\"color:#FF0000;\">"+i+"</strong>");
			}else
				navigation.append("<a href=javascript:free_GoPage('" + i + "');>" + i + "</a>");
		}
		
		//다음블럭 첫페이지로
		if((cPage>lastPage) || (currentBlock <totalBlock) ){
			navigation.append("<a href=\"javascript:free_GoPage('" + (((currentBlock+1)*blockSize)-(blockSize-1 )) + "');\" ><img src=\"" + "/img/mms/btn_page_last.gif\" alt=\"마지막페이지\" width=\"14\" height=\"12\" align=\"absmiddle\" /></a>");
		}
		
		//마지막 페이지로
		if(currentBlock <totalBlock){
			navigation.append("<a href=\"javascript:free_GoPage('" + totalPage + "');\" ><img src=\"" + "/img/mms/btn_page_last.gif\" alt=\"마지막페이지\" width=\"14\" height=\"12\" align=\"absmiddle\" /></a>");
		}
		logger.debug(navigation);
		return navigation.toString();
		
	}
}
